package com.hopes.bizz.risk.integral.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hopes.bizz.risk.integral.bo.IntegralEvent;
import com.hopes.bizz.risk.integral.entity.RiskIntegralEventEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yumr
 * @Description 积分事件处理接口
 * @time 2023/8/17 18:06
 */
public interface IntegralEventHandler<T> {

    IService<T> getService();

    void handler(IntegralEvent integralEvent);

    IService<RiskIntegralEventEntity> getEventService();

    default Map<Integer, Integer> integralEventMap() {
        final List<RiskIntegralEventEntity> eventList = getEventService().list();
        if (CollUtil.isEmpty(eventList)) {
            return new HashMap<>();
        }
        return eventList.parallelStream().filter(e ->
                Objects.nonNull(e.getEventName())).collect(Collectors.toMap(RiskIntegralEventEntity::getEventName, RiskIntegralEventEntity::getIntegral));
    }

    default boolean saveIntegralRecordBatch(List<T> entityList) {
        return this.getService().saveBatch(entityList);
    }

}
